<?php
/**
 * 员工管理
 *
 * @author 孙晓晔
 * @version $Id: Admin.class.php 1053 2010-04-07 11:15:15Z sunxy $s
 */

require_once(dirname(__FILE__) . '/Emp.class.php');

class Admin extends Emp {

	function __construct() {
		$request['update'] = '$Date: 2009/03/06 11:35:48 $';
		$request['revision'] = '$Revision: 1.2 $';

		parent::__construct($request);
	}
			
	function copy($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'emp/emp/admin/copy');
		return true;
	}
	
	function copyPost($controller, $request) {
		$this->pretreat($controller);
		
		$from = $request['from_emp'];		
		$to = $this->_input($request['to_emp']);
		
		$this->_copy_priv($from, $to);
		$this->_copy_role($from, $to);
		$this->_copy_service($from, $to);
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}
	
	function _copy_priv($from, $to) {
		require_once(dirname(__FILE__) . '/EmpPriv.class.php');
		$aa = new EmpPriv();
		$aa->_copy($from, $to);
	}
	
	function _copy_role($from, $to) {
		require_once(dirname(__FILE__) . '/EmpRole.class.php');
		$aa = new EmpRole();	
		$aa->_copy($from, $to);
	}
	
	function _copy_service($from, $to) {
		require_once(dirname(__FILE__) . '/EmpService.class.php');
		$aa = new EmpService();	
		$aa->_copy($from, $to);	
	}
	
	function move($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];
		
		require_once 'Emp.class.php';
		$emp = new Emp();
		$controller->assign_by_ref('emp', $emp->_view($id));

		$controller->display($request, 'emp/emp/admin/move');
		return true;
	}
	
	function movePost($controller, $request) {
		$this->pretreat($controller);
		
		$from = $request['from_emp'];		
		$to = $this->_input($request['to_emp']);
		
		$this->_move('fy.emp_priv', $from, $to);
		$this->_move('fy.emp_role', $from, $to);
		$this->_move('fy.emp_service', $from, $to);

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;		
	}
	
	function _move($tbl, $from, $to) {
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET emp = '%s' WHERE emp = '%s'";
		$sql = sprintf($fmt, $tbl, $to, $from);
		debug($sql);
		$db->query($sql);
	}
	
	function connect($controller, $request) {
		$this->pretreat($controller);
		
		$id = $request['id'];
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);
		
		$type = array(
			'-' => '请选择',
			'1' => '市场部',		
			'2' => '工程师',
			'3' => '采购部',
		
			'9' => '公共',
		);
		$controller->assign_by_ref('type_option', $type);

		$controller->display($request, 'emp/emp/admin/connect');
		return true;
	}
	
	function connectPost($controller, $request) {
		$this->pretreat($controller);
		
		$type = $request['type'];
		$from = $request['from_emp'];		
		$to = $this->_input($request['to_emp']);
		
		$this->_connect_c($from, $to);
		
		if ($type == '1') {
			$this->_connect_1($from, $to);
		}
		
		if ($type == '2') {
			$this->_connect_2($from, $to);
		}
		
		if ($type == '3') {
			$this->_connect_3($from, $to);
		}
		
		if ($type == '9') {
			$this->_connect_9($from, $to);
		}
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;		
	}
	
	function _connect_c($from, $to) {
		require_once(dirname(__FILE__) . '/Mac.class.php');
		$mac = new Mac();
		$mac->move($from, $to);
		
		require_once(dirname(__FILE__) . '/Exten.class.php');
		$exten = new Exten();
		$exten->move($from, $to);
		
		require_once(dirname(__FILE__) . '/../finance/Asset.class.php');
		$asset = new Asset();
		$asset->move($from, $to);
		
		require_once(dirname(__FILE__) . '/../contact/Supplier.class.php');
		$supplier = new Supplier();
		$supplier->move($from, $to);
		
		require_once(dirname(__FILE__) . '/../finance/Payment.class.php');
		$payment = new Payment();
		$payment->move($from, $to);
		
		require_once(dirname(__FILE__) . '/../info/Note.class.php');
		$note = new Note();
		$note->move($from, $to);
		
		require_once(dirname(__FILE__) . '/../info/Customize.class.php');
		$customize = new Customize();
		$customize->move($from, $to);
	}
	
	function _connect_1($from, $to) {
		require_once(dirname(__FILE__) . '/../contact/Customer.class.php');
		$customer = new Customer();
		$customer->move($from, $to);
		
		require_once(dirname(__FILE__) . '/../customer/Quote.class.php');
		$quote = new Quote();
		$quote->move_emp($from, $to);
		
		require_once(dirname(__FILE__) . '/../mold/Mold.class.php');
		$mold = new Mold();
		$mold->move($from, $to);
		
		require_once(dirname(__FILE__) . '/../express/Requirement.class.php');
		$express_req = new Requirement();
		$express_req->move($from, $to);
		
		require_once(dirname(__FILE__) . '/../finance/Transaction.class.php');
		$tr = new Transaction();
		$tr->move($from, $to);
		
		require_once(dirname(__FILE__) . '/../finance/In.class.php');
		$in = new In();
		$in->move($from, $to);
		
		require_once(dirname(__FILE__) . '/../finance/Out.class.php');
		$out = new Out();
		$out->move($from, $to);
	}
	
	function _connect_2($from, $to) {		
		require_once(dirname(__FILE__) . '/../mold/Mold.class.php');
		$mold = new Mold();
		$mold->move_engineer($from, $to);
		
		require_once(dirname(__FILE__) . '/../finance/Out.class.php');
		$out = new Out();
		$out->move($from, $to);
	}
	
	function _connect_3($from, $to) {
		require_once(dirname(__FILE__) . '/../material/Purchase.class.php');
		$purchase = new Purchase();
		$purchase->move($from, $to);
		
		require_once(dirname(__FILE__) . '/../finance/Out.class.php');
		$out = new Out();
		$out->move($from, $to);
	}
	
	function _connect_9($from, $to) {		
		$this->_connect_c($from, $to);
	}

	/**
	 * 判断用户是否有权限，若有权限，则返回true
	 */
	function isValid($controller, $request) {
		$action = $request['do'];
		$cc = $this->_cc($_SERVER["SCRIPT_NAME"]);
		
		$emp = $_SESSION['id'];

		return $this->_valid_a($emp, $cc, $action);
	}

	function _cc($uri) {
		$pos = strrpos($uri, "/");
		if ($pos === false) {
			return $uri;
		}
		
		return substr($uri, $pos + 1);
	}

	function _valid_a($emp, $cc, $action) {
		$fmt = "emp = %s, cc = %s, action = %s";
		// debug(sprintf($fmt, $emp, $cc, $action));
		
		$key = "valid_" . $emp . "_" . $cc . $action;
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if ($this->_true($ret)) {
				return $ret;
			}
		}

		$ret = $this->_valid_b($emp, $cc, $action);
		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("1 day"));
		}

		return $ret;
	}

	function _true($s) {
		if (is_null($s)) {
			return false;
		}

		if (strlen($s) == 0) {
			return false;
		}

		if (is_bool($s)) {
			return $s;
		}

		return $s;
	}

	function _valid_b($emp, $cc, $action) {
		require_once(dirname(__FILE__) . '/../emp/EmpRole.class.php');
		$aa = new EmpRole();
		
		return $aa->_valid_b($emp, $cc, $action);
	}

	/**
	 * 群组权限判断
	 *
	 * @param string
	 * @param string
	 * @return boolean 是否有权限
	 */	
	function role_priv($role, $cc, $action) {
		
		require_once(dirname(__FILE__) . '/../info/RolePriv.class.php');
		$aa = new RolePriv();
		
		$aa->_priv_role($role, $cc, $action);		
	}

	/**
	 * 用户权限判断
	 *
	 * @param string
	 * @param string
	 * @return boolean 是否有权限
	 */
	function emp_priv($emp, $cc, $action) {
		require_once(dirname(__FILE__) . '/../emp/EmpPriv.class.php');
		$aa = new EmpPriv();
		
		$aa->_emp_priv($emp, $cc, $action);
	}

	/**
	 * 接口
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_dept($controller, $request) {
		$emp = $_SESSION['id'];
		$dept = $request['dept'];

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE cc = '%s' AND status = '1' ORDER BY orderby";
		$sql = sprintf($fmt, Constant::tbl_priv, $dept);
		// debug($sql);
		$result = $db->query($sql);
		$data = array();
		$data1 = array();
		while($row = $db->fetch_array($result)) {
			if ($this->priv_emp($row, $id)) {
				$data[] = $row;
			} else {
				$data1[] = $row;
			}
			//$data1[] = $row;
		}
		$controller->assign('result', json_encode($data1));

		$request['tpl'] = 'json';
		$controller->display($request, 'json');
		return true;
	}

	/**
	 * 接口
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_el($controller, $request) {
		$emp = $_SESSION['id'];
		$dept = $request['dept'];

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE cc = '%s' AND status = 1 ORDER BY orderby";
		$sql = sprintf($fmt, Constant::tbl_priv, $dept);
		// debug($sql);
		$result = $db->query($sql);
		$data = array();
		$data1 = array();
		while($row = $db->fetch_array($result)) {
			if ($this->priv_emp1($row['cc'], $id)) {
				$data[] = $row;
			} else {
				$data1[] = $row;
			}
			//$data1[] = $row;
		}
		$controller->assign('result', json_encode($data));

		$request['tpl'] = 'json';
		$controller->display($request, 'json');
		return true;
	}

	function t($controller, $request) {
		$s = "adfamold.php";

		// $s = "http://localhost/t/tt.php";

		// $a = parse_url($s);

		// print_r($a);
		$pos = strrpos($s, "/");
		if ($pos === false) {
			echo "OK.";
		} else {
			echo substr($s, $pos + 1);
		}

		logdate($controller, $request);
		return true;
	}
}
?>
